//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CompositeConstraint.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (Cephei::QL::Math::Optimization::IConstraint^ c1, Cephei::QL::Math::Optimization::IConstraint^ c2) : CConstraint(CCompositeConstraint::typeid)
{
    CConstraint^ _Cc1;
    CConstraint^ _Cc2;
    try
    {
#ifdef HANDLE
        _phCompositeConstraint = NULL;
#endif
        _Cc1 = safe_cast<CConstraint^> (c1);
        _Cc1->Lock();
        QuantLib::Constraint& _c1 = static_cast<QuantLib::Constraint&> (_Cc1->GetReference ()); 
        _Cc2 = safe_cast<CConstraint^> (c2);
        _Cc2->Lock();
        QuantLib::Constraint& _c2 = static_cast<QuantLib::Constraint&> (_Cc2->GetReference ()); 
        _ppCompositeConstraint = new boost::shared_ptr<QuantLib::CompositeConstraint> (new QuantLib::CompositeConstraint ( _c1,  _c2 ));
        SetConstraint (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppCompositeConstraint));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cc1 != nullptr) _Cc1->Unlock();
        if (_Cc2 != nullptr) _Cc2->Unlock();
    }
}
Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (boost::shared_ptr<QuantLib::CompositeConstraint>& childNative, Object^ owner) : CConstraint(CCompositeConstraint::typeid)
{
#ifdef HANDLE
	_phCompositeConstraint = NULL;
#endif
	_ppCompositeConstraint = &childNative;
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppCompositeConstraint));
}
Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (QuantLib::CompositeConstraint& childNative, Object^ owner) : CConstraint(CCompositeConstraint::typeid)
{
#ifdef HANDLE
	_phCompositeConstraint = NULL;
#endif
	_ppCompositeConstraint = new boost::shared_ptr<QuantLib::CompositeConstraint> (&childNative);
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppCompositeConstraint));
    _CompositeConstraintOwner = owner;
    _ConstraintOwner = owner;
}

Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (CCompositeConstraint^ copy) : CConstraint(CCompositeConstraint::typeid)
{
#ifdef HANDLE
	_phCompositeConstraint = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCompositeConstraint = new boost::shared_ptr<QuantLib::CompositeConstraint> (copy->GetShared());
        _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppCompositeConstraint));
    }
}
Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (System::Type^ t) : CConstraint(CCompositeConstraint::typeid)
{
#ifdef HANDLE
	_phCompositeConstraint = NULL;
#endif
	if (!t->IsSubclassOf(CCompositeConstraint::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (QuantLib::Handle<QuantLib::CompositeConstraint>& childNative, Object^ owner)  : CConstraint(CCompositeConstraint::typeid)
{
	_phCompositeConstraint = &childNative;
	_ppCompositeConstraint = &static_cast<boost::shared_ptr<QuantLib::CompositeConstraint>>(childNative.currentLink());
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppCompositeConstraint));
    _CompositeConstraintOwner = owner;
}
Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (QuantLib::Handle<QuantLib::CompositeConstraint> childNative)  : CConstraint(CCompositeConstraint::typeid)
{
	_phCompositeConstraint = &childNative;
	_ppCompositeConstraint = &static_cast<boost::shared_ptr<QuantLib::CompositeConstraint>>(childNative.currentLink());
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppCompositeConstraint));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CCompositeConstraint::CCompositeConstraint (QuantLib::CompositeConstraint childNative)  : CConstraint(CCompositeConstraint::typeid)
{
#ifdef HANDLE
	_phCompositeConstraint = NULL;
#endif
	_ppCompositeConstraint = new boost::shared_ptr<QuantLib::CompositeConstraint> (new QuantLib::CompositeConstraint (childNative));
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppCompositeConstraint));
}
#endif

Cephei::QL::Math::Optimization::CCompositeConstraint::~CCompositeConstraint ()
{
    if (_ppCompositeConstraint != NULL)
    {
	    delete _ppCompositeConstraint;
        _ppCompositeConstraint = NULL;
    }
}
Cephei::QL::Math::Optimization::CCompositeConstraint::!CCompositeConstraint ()
{
    if (_ppCompositeConstraint != NULL)
    {
	    delete _ppCompositeConstraint;
    }
}
QuantLib::CompositeConstraint& Cephei::QL::Math::Optimization::CCompositeConstraint::GetReference ()
{
    if (_ppCompositeConstraint == NULL) throw gcnew NativeNullException ();
	return **_ppCompositeConstraint;
}
boost::shared_ptr<QuantLib::CompositeConstraint>& Cephei::QL::Math::Optimization::CCompositeConstraint::GetShared ()
{
    if (_ppCompositeConstraint == NULL) throw gcnew NativeNullException ();
	return *_ppCompositeConstraint;
}
QuantLib::CompositeConstraint* Cephei::QL::Math::Optimization::CCompositeConstraint::GetPointer ()
{
    if (_ppCompositeConstraint == NULL) throw gcnew NativeNullException ();
	return &**_ppCompositeConstraint;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CompositeConstraint>& Cephei::QL::Math::Optimization::CCompositeConstraint::GetHandle ()
{
	if (_phCompositeConstraint == NULL)
	{
		_phCompositeConstraint = new Handle<QuantLib::CompositeConstraint> (*_ppCompositeConstraint);
	}
	return *_phCompositeConstraint;
}
#endif
bool Cephei::QL::Math::Optimization::CCompositeConstraint::HasNative () 
{
	return (_ppCompositeConstraint != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::ICompositeConstraint^ Cephei::QL::Math::Optimization::CCompositeConstraint_Factory::Create (Cephei::QL::Math::Optimization::IConstraint^ c1, Cephei::QL::Math::Optimization::IConstraint^ c2)
{
    return gcnew CCompositeConstraint ( c1,  c2);
}
